﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Furion;
using Furion.DynamicApiController;
using Furion.UnifyResult;
using LNSServer.Models;
using LNSServer.ViewModels.ResourcePool;
using Mapster;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using SqlSugar;

namespace LNSServer.Controllers
{
    /// <summary>
    /// 资源库
    /// </summary>
    [DynamicApiController]
    public class LnsResourcePoolController
    {
        private readonly ILogger<LnsResourcePoolController> logger;
        private readonly ISqlSugarClient db; // 核心对象：拥有完整的SqlSugar全部功能
        public LnsResourcePoolController(ISqlSugarClient sqlSugarClient)
        {
            this.db = sqlSugarClient;
            this.logger = logger;
        }

        /// <summary>
        /// 模型列表
        /// </summary>
        /// <param name="mxsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> List([FromBody] ResourcePoolSearchForm mxsf)
        {
            RefAsync<int> total = 0;
            List<ResourcePool> moxings = await db.Queryable<ResourcePool>()
                .Mapper(dt => dt.User, dt => dt.UId, dt => dt.User.Id)
                .Where(mx => mx.IsActive == 1)
                .WhereIF(!string.IsNullOrEmpty(mxsf.Content), mx => mx.Title.Contains(mxsf.Content))
                //.WhereIF(mxsf.TgId != 0, mx => mx.TgId == mxsf.TgId)
                .WhereIF(!string.IsNullOrEmpty(mxsf.TagName), mx => mx.TagName == mxsf.TagName)
                .WhereIF(mxsf.UId != 0, mx => mx.UId == mxsf.UId)
                .ToPageListAsync(mxsf.PageIndex, mxsf.PageSize, total);

            UnifyContext.Fill(new { Total = total.Value });
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = moxings.Adapt<List<ResourcePoolView>>(),
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 增加模型
        /// </summary>
        /// <param name="mxsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Save([FromBody] ResourcePoolSaveForm mxsf)
        {
            await db.Insertable<ResourcePool>(mxsf.Adapt<ResourcePool>()).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = mxsf,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 删除模型
        /// </summary>
        /// <param name="mxdf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Delete([FromBody] ResourcePoolDeleteForm mxdf) {
            await db.Updateable<ResourcePool>().SetColumns(mx => mx.IsActive == 0).Where(mx => mx.Id == mxdf.Id).ExecuteCommandAsync();

            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = null,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 修改模型
        /// </summary>
        /// <param name="mxuf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Update([FromBody] ResourcePoolUpdateForm mxuf) {
            await db.Updateable<ResourcePool>(mxuf.Adapt<ResourcePool>()).ExecuteCommandAsync();
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = mxuf,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

        /// <summary>
        /// 查看模型
        /// </summary>
        /// <param name="mxsf"></param>
        /// <returns></returns>
        [HttpPost]
        public async Task<IActionResult> Show([FromBody] ResourcePoolShowForm mxsf) {
            ResourcePool moxing = await db.Queryable<ResourcePool>()
                .InSingleAsync(mxsf.Id);
            return new JsonResult(new RESTfulResult<object>
            {
                StatusCode = 200,
                Succeeded = true,
                Data = moxing,
                Errors = null,
                Extras = UnifyContext.Take(),
                Timestamp = DateTimeOffset.UtcNow.ToUnixTimeMilliseconds()
            });
        }

    }
}
